/***************************************************************************
 *   Copyright (C) 2008 by Tomasz Dubik                                    *
 *   tdubik@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "task.h"
#include <QDebug>
Task::Task()
{
	m_type = (TaskType) -1;
	m_priority = (TaskPriority) -1;
	m_percent = 0;
}

Task::Task(const Task& task)
{
    m_task = task.taskTxt();
    m_date = task.date();
    m_type = task.type();
    m_priority = task.priority();
    m_percent = task.percent();
}

Task::Task(const QString& task,
		    const QDate& date,
	   		const TaskType& type,
	   		const TaskPriority& priority,
	   		int percent):
			m_task(task)
			,m_date(date)
			,m_type(type)
			,m_priority(priority)
			,m_percent(percent)
{
	//not implemented
}

void Task::init()
{
    m_type = eWork;
    m_priority = eLow;
    m_percent = 0;
    m_date = QDate::currentDate();
}

void Task::setTaskTxt(const QString& task)
{
	m_task = task;
}

void Task::setDate(const QDate& date)
{
	m_date = date;
}

void Task::setType(const TaskType& type)
{
	m_type = type;
}

void Task::setPriority(const TaskPriority& priority)
{
	m_priority = priority;
}

void Task::setPercent(const int percent)
{
	m_percent = percent;
}

const QString& Task::taskTxt()const
{
	return m_task;
}

const QDate& Task::date()const
{
	return m_date;

}

const TaskType& Task::type()const
{
	return m_type;
}

const TaskPriority& Task::priority()const
{
	return m_priority;
}

const int Task::percent()const
{
	return m_percent;
}

int Task::internalize(QString& line)
{
	int i = 0, j = 0;
	bool ok = false;
	while ((j = line.indexOf(TaskSeparator,i)) != -1)
	{
		if( m_priority < 0 )
		{
			ok = false;
			m_priority = (TaskPriority)line.mid(i,j-i).toInt(&ok);
			if(!ok)
			{
				qDebug() << "file corrupted:priority not set";
				return -1;
			}
		}
		else if( m_task.isEmpty() )
			m_task = line.mid(i,j-i);
		else if( m_date.isNull())
		{
			m_date = QDate::fromString(line.mid(i,j-i), DateFormat);
			if(!m_date.isValid())
				m_date = QDate::currentDate();
		}
		else if( m_type < 0 )
		{
			ok = false;
			m_type = (TaskType)line.mid(i, j-i).toInt(&ok);
			if( !ok )
			{
				qDebug() << "file corrupted:type not set";
				return -1;
			}
		}
		else
		{
			ok = false;
			m_percent = (TaskType)line.mid(i, j-i).toInt(&ok);
			if( !ok )
			{
				qDebug() << "file corrupted:percent not saved";
				return -1;
			}
		}

	i = j + TaskSeparatorLength;
	}
	return 0;
}

void Task::externalize(QTextStream &stream)
{
	stream << (int) m_priority;
	stream << TaskSeparator;
	stream << m_task;
	stream << TaskSeparator;
	stream << m_date.toString(DateFormat);
	stream << TaskSeparator;
	stream << (int) m_type;
	stream << TaskSeparator;
	stream << m_percent;
	stream << TaskSeparator << EndOfTask;
}

void Task::copy( Task& task )
{
	setTaskTxt( task.taskTxt() );
	setDate( task.date() );
	setType( task.type() );
	setPriority( task.priority() );
	setPercent( task.percent() );
}
//EOF
